package com.dawn.modules.chat.tools;


import com.dawn.common.utils.UserThreadLocal;
import com.dawn.modules.chat.entity.Note;
import com.dawn.modules.chat.entity.Notebook;
import com.dawn.modules.chat.service.NotebookService;
import com.dawn.modules.chat.service.NoteService;
import jakarta.annotation.Resource;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Component
public class NoteTools {
    @Resource
    private NoteService noteService;
    @Resource
    private NotebookService notebookService;
    @Tool(name = "判断用户是否有笔记本", description = "判断该用户是否有笔记本")
    public String hasNotebook() {
        Integer userId = UserThreadLocal.getUser();
        Notebook notebook1 = new Notebook();
        notebook1.setUserId(userId);
        List<Notebook> noteBookByUserId = notebookService.getNoteBook(notebook1);
        if (noteBookByUserId == null || noteBookByUserId.isEmpty())
            return "该用户没有笔记本";
        StringBuilder res = new StringBuilder();
        res.append("你现在拥有笔记本如下：");
        for (Notebook notebook : noteBookByUserId) {
            res.append(notebook.getNotebookName()).append("、");
        }
        return res.toString();
    }
    @Tool(name = "创建笔记本", description = "你不得自己指定笔记本名称，必须向用户请求笔记本名称")
    public String createNoteBook(String noteBookName) {
        Integer userId = UserThreadLocal.getUser();
        Boolean flag = notebookService.addNoteBook(userId, noteBookName);
        if (flag) {
            return "笔记本创建成功";
        }
        return "笔记本创建失败";
    }
    @Tool(name = "添加笔记", description = "先调用工具方法hasNotebook判断用户是否有笔记本。" +
            "第若用户有笔记本，则询问用户使用哪一个笔记本; 若用户没有笔记本，则调用工具方法createNoteBook创建笔记本")
    public String addNote(String noteBookName,
                           @ToolParam(description = "该笔记名称，即用户的提问") String noteTitle,
                           @ToolParam(description = "该笔记内容，即你的回答") String noteContent) {
        Integer userId = UserThreadLocal.getUser();
        Integer noteId = noteService.addNote(userId, noteBookName, noteTitle, noteContent);
        Boolean flag = notebookService.addNoteToNoteBook(userId, noteBookName, noteId);
        if (flag) {
            return "笔记添加成功";
        }
        return "笔记添加失败";
    }
    @Tool(name = "查询所有笔记", description = "根据参数，查询用户该笔记本下的所有笔记")
    public Map<String, String> getNotes(String noteBookName) {
        Integer userId = UserThreadLocal.getUser();
        List<Notebook> noteBook = notebookService.getNoteBook(Notebook.builder()
                .userId(userId)
                .notebookName(noteBookName)
                .build());
        String noteIds = noteBook.get(0).getNoteIds();
        String[] split = noteIds.split(",");
        String[] split1 = new String[split.length / 2];
        for (int i = 0; i < split.length; i += 2) {
            split1[i / 2] = split[i];
        }
        List<Integer> noteIdList = Arrays.stream(split1).map(Integer::parseInt).collect(Collectors.toList());
        List<Note> notes = noteService.listByIds(noteIdList);
        Map<String, String> res = new HashMap<>();
        for (Note note : notes) {
            res.put(note.getTitle(), note.getContent());
        }
        return res;
    }
}
